﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace CodeScrapWindow
{
	internal sealed class GeneratedCodeTraceListener : TraceListener
	{
		/// <summary>
		/// Occurs when the value of Message has changed.
		/// </summary>
		public event EventHandler MessageChanged;

		private string _message;

		/// <summary>
		/// Gets or sets the message.
		/// </summary>
		/// <value>The message.</value>
		public string Message
		{
			get
			{
				return _message;
			}
			set
			{
				if (_message != value)
				{
					_message = value;
					OnMessageChanged(EventArgs.Empty);
				}
			}
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="GeneratedCodeTraceListener"/> class.
		/// </summary>
		public GeneratedCodeTraceListener()
		{
		}

		/// <summary>
		/// When overridden in a derived class, writes the specified message to the listener you create in the derived class.
		/// </summary>
		/// <param name="message">A message to write.</param>
		public override void Write(string message)
		{
			this.Message = message;
		}

		/// <summary>
		/// When overridden in a derived class, writes a message to the listener you create in the derived class, followed by a line terminator.
		/// </summary>
		/// <param name="message">A message to write.</param>
		public override void WriteLine(string message)
		{
			this.Message = message + Environment.NewLine;
		}

		private void OnMessageChanged(EventArgs e)
		{
			EventHandler changed = MessageChanged;
			if (changed != null)
			{
				changed(this, e);
			}
		}
	}
}
